// pages/account/index.js
var app = getApp(), fetchApi = require("../../utils/httpPost.js");
import Notify from '../../component/vant/notify/notify';
import Toast from '../../component/vant/toast/toast';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    phoneNum:'',
    user:{},
    isShowPopupEstate:false,
    action:'',
    from_type:'',
    nickName:'',
    faceUrl:'',
    user_id:0,
    isShowPop:false,
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log('account ',options)
    if(options.from_type){
      this.data.from_type = options.from_type;
    }
    this.data.user_id = app.getStorageSync("user_id");
    this.onHttpInit();
  },
  /**
   * 获取页面数据的 http请求
   */
  onHttpInit: function () {
    var that = this;
    fetchApi.httpPost('/user/getAccountInfo', {}, function (res) {
      console.log("账号绑定 数据", res)
      if(res.data.code == 0){
        var phone_num = res.data.data.phone_num;
        if(phone_num!=undefined && phone_num!=''){
          app.setStorageSync("phone_num", phone_num);
        }
        phone_num = phone_num!=undefined && phone_num!='' ? phone_num : '';
        that.setData({
          nickName: res.data.data.nick_name,
          faceUrl:  res.data.data.face_url,
          phoneNum: phone_num,
        })
      }
      that.setData({
        netWork: true,
      })
    }, function () {
        that.setData({ netWork: false, hasData: false })
    })
  },
  onChooseAvatar(e){
    var that = this;
    const { avatarUrl } = e.detail ;
    console.log('avatarUrl',avatarUrl);
    if(avatarUrl!=undefined && avatarUrl !=''){
      wx.uploadFile({
        url: fetchApi.getPlatformUrl() + '/user/updateFaceUrl',
        filePath: avatarUrl,
        name: 'file',
        formData: { user_id: that.data.user_id },
        success: function (res) {
          console.log('uploadFile res', res)
          var jsonData = JSON.parse(res.data);
          console.log('jsonData',jsonData)
          if(jsonData.code == 0){
            Toast.success('上传成功');
            that.setData({
              faceUrl: jsonData.data,
            })
            app.setStorageSync("face_url",jsonData.data);
          }else{
            Toast.fail('上传失败');

          }
        }, fail: function (res) {
          Toast.fail('上传失败');
          console.log('uploadFile fail res', res)
        }
      })
    }
  },
  uploadNickName(){
    this.setData({
      isShowPop : !this.data.isShowPop,
    })
  },
  inputNickName(e){
    var nickName = e.detail;
    this.data.nickName = nickName;
  },
  goUpdateNickName(){
    if(this.data.nickName == ''){
      Toast("请输入昵称");
      return;
    }

    var that = this;
    fetchApi.httpPost('/user/updateNickName', {
      nickName: that.data.nickName,
    }, function (res) {
      console.log("修改昵称 数据", res)
      if(res.data.code == 0){
        Toast.success(res.data.msg);
        that.setData({
          nickName: that.data.nickName,
          isShowPop : false,
        })
        app.setStorageSync("nick_name",that.data.nickName);
      }else{
        Toast.fail(res.data.msg);
      }
    }, function () {
    })
  },
  onClickEstate() {
    console.log('onClickEstate')
    wx.navigateTo({
      url: '/pages/estate/list/index',
    })
  },
  getPhoneNumber(e){
    var that = this;
    fetchApi.httpPost('/user/getWxPhoneNum', { 
      code: e.detail.code, 
      user_type : 0,
    }, function (res) {
        if(res.data.code == 0){
          Toast.success(res.data.msg)
          that.setData({
            phone_num: res.data.data
          })
          app.setStorageSync("phone_num", res.data.data);
          if(that.data.from_type!=''){
            setTimeout(function () {
              wx.navigateBack({
                delta: 1,
              })
            }, 800)
          }
        }else{
          Toast.fail(res.data.msg)
        }
    });
  },
  onShow(options){
    console.log('onShow options',options)
  }
})